#include "Logger.hpp"
#include "Config.hpp"

using namespace Log;

Logger* Logger::sLogger = NULL;

Logger::CleanUp::CleanUp()
{
   	
}	

Logger::CleanUp::~CleanUp()
{
   delete sLogger;	
}

Logger& Logger::getInstance()
{
   static CleanUp cleanUp;
   if(sLogger == NULL){
       Config::loadConfig();
       //static Logger::CleanUp CLEANUP;
       sLogger = new Logger();
   }
   return *sLogger;
}

Logger::~Logger()
{
   //if(fileout.good()){
	   fileout.close();
   //}
}
const Logger& Logger::log(const std::string message, LogLevl level)
{
	if(level == Debug){
		fileout << "DEBUG:";
  }else if(level == Info){
  	fileout << "INFO:";
  }else if(level == Warn){
  	fileout << "WARN:";
  }else if(level == Error){
  	fileout << "ERROR:";
  }
  boost::posix_time::ptime now(boost::posix_time::second_clock::local_time());
	fileout << now << "\n";	
	fileout << "    " << message << std::endl;
}

Logger::Logger()
{
	fileout.open(Config::getConfigStr("logfile").c_str(), ios_base::app);
	if(!fileout.good()){
		std::cerr<<"fileout is error!"<<std::endl;
		//throw runtime_error("can not open file!");
  }
}